********************************************************************************
** 	TITLE:		CA11_results                                                  ** 	
**	AUTHOR:	    Philippe Mongrain											  **
**	DATA:       table_tableau12_2011                                          **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Import the dataset

copy https://www.elections.ca/scripts/OVR2011/34/data_donnees/table_tableau12.csv table_tableau12_2011.csv, replace
type table_tableau12_2011.csv
import delimited table_tableau12_2011.csv, clear

* Rename variables

rename electoraldistrictnamenomdecircon districtname11
rename electoraldistrictnumbernumérodec districtnumber11
rename candidatecandidat candidate11
rename candidateresidencerésidenceducan residence11
rename candidateoccupationprofessionduc occupation11
rename votesobtainedvotesobtenus numberofvotes11
rename percentageofvotesobtainedpourcen numberofvotespercent11
rename majoritymajorité majoritynumber11
rename majoritypercentagepourcentagedem majoritypercent11

* Generate dichotomous variable for each party 

gen lib11 = strpos(candidate11, "Liberal/Libéral") > 0
gen con11 = strpos(candidate11, "Conservative/Conservateur") > 0
gen ndp11 = strpos(candidate11, "NDP-New Democratic Party/NPD-Nouveau Parti démocratique") > 0
gen green11 = strpos(candidate11, "Green Party/Parti Vert") > 0
gen bq11 = strpos(candidate11, "Bloc Québécois/Bloc Québécois") > 0

* Generate party variable

gen party11 = 1 if lib11 == 1
replace party11 = 2 if con11 == 1
replace party11 = 3 if ndp11 == 1
replace party11 = 4 if bq11 == 1
replace party11 = 0 if party11 == . 

* Generate rank of parties

gsort districtname11 -numberofvotes11
bysort districtname11 : gen rank11 = _n

gen first11 = party11 if rank11 == 1
gen second11 = party11 if rank11 == 2
gen third11 = party11 if rank11 == 3

bysort districtname11 : gen winner11 = first11[1]
bysort districtname11 : gen runnerup11 = second11[2]
bysort districtname11 : gen thirdplace11 = third11[3]

* Generate margins of victory

bysort districtname11 : gen margin11 = numberofvotespercent11[1] - numberofvotespercent11[2]

* Generate effective number of electoral parties

gen sqrvotepr11 = (numberofvotespercent11/100)^2

bysort districtname11 : egen sumsqrvotepr11 = sum(sqrvotepr11)

gen enep11 = 1/sumsqrvotepr11

* Labelling parties

label define party 0 "Other" 1 "Liberal Party" 2 "Conservative Party" 3 "NDP" 4 "Bloc Quebecois"
label values winner11 runnerup11 thirdplace11 party

* Selecting, ordering, and sorting variables

keep districtnumber11 districtname11 party11 rank11 numberofvotes11 numberofvotespercent11 margin11 enep11 winner11 runnerup11 thirdplace11

order districtnumber11 districtname11 party11 rank11 numberofvotes11 numberofvotespercent11 margin11 enep11 winner11 runnerup11 thirdplace11

sort districtnumber11 rank11

drop if rank11 > 3

* Saving file

save CA11_results, replace

* Drop duplicates

duplicates tag districtnumber11, gen(dup)
duplicates drop districtnumber11, force
drop dup

* Merge with CA08_results.dta

merge 1:1 _n using CA08_results.dta

rename _merge _merge08_11

* Generate variable for incumbent party reelection

gen reelected11 = 1 if winner11 == winner08
replace reelected11 = 0 if reelected11!=1

* Create election variable

gen election11 = "2011 Canadian federal election"

* Saving file

save CA0811_results, replace